﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;

namespace SetSailUpgrade
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                timer1.Enabled = false;

                if (File.Exists(Environment.CurrentDirectory + "\\setup.zip"))
                {
                    string error = "";
                    bool flag = ZipHelper.UnZipFile(Environment.CurrentDirectory + "\\setup.zip", Environment.CurrentDirectory, out error);
                    if (flag)
                    {
                        File.Delete(Environment.CurrentDirectory + "\\setup.zip");
                        label1.Text = "升级成功...正在重新启动";
                    }
                    else
                    {
                        label1.Text = error.Trim();

                    }
                    Thread.Sleep(2000);
                    Application.ExitThread();
                    //启动运行程序
                    ProcessStartInfo psi = new ProcessStartInfo();
                    psi.FileName = Environment.CurrentDirectory + "\\SetSailReadFileTools.exe";
                    psi.UseShellExecute = false;
                    psi.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
                    psi.CreateNoWindow = true;
                    Process.Start(psi);
                }
                else
                {
                    label1.Text = "未查找到升级安装包，请联系管理员...";
                }
                //string path = Environment.CurrentDirectory + "\\SetSailSetUp.msi";
                //bool flag2 = WindowsHelper.CheckIfAppInstall("SetSailSetUp");
                //string sysroot = System.Environment.SystemDirectory;
                //if (flag2)
                //{
                //    label1.Text = "正在卸载旧版本...";
                //    Thread.Sleep(500);
                //    Process prc = Process.Start(sysroot + "\\msiexec.exe ", "/x {07B75016-66C7-481B-9AE6-66FD6677F740} /qn");
                //    prc.WaitForExit();
                //}
                //label1.Text = "正在安装新版本...";
                //Thread.Sleep(500);
                //string run_path = Application.StartupPath + "\\SetSailSetUp.msi";

                //string order = "/i " + run_path + " TARGETDIR=\"" + path.Replace("\\", "\\\\") + "\" /qn";
                //Process prc2 = Process.Start(sysroot + "\\msiexec.exe ", order);
                //prc2.WaitForExit();
                ////if (File.Exists(run_path))
                ////    File.Delete(run_path);
                //Application.ExitThread();
                ////启动运行程序
                //ProcessStartInfo psi = new ProcessStartInfo();
                //psi.FileName = Environment.CurrentDirectory + "\\SetSailReadFileTools.exe";
                //psi.UseShellExecute = false;
                //psi.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
                //psi.CreateNoWindow = true;
                //Process.Start(psi);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
